package com.yj.rselasticsearch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yj.common.config.RuoYiConfig;
import com.yj.common.constant.DataConstants;
import com.yj.common.core.controller.BaseController;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.FileInfo;
import com.yj.common.utils.FastUtils;
import com.yj.common.utils.file.FileUtils;
import com.yj.rselasticsearch.domain.dto.FileInfoDto;
import com.yj.rselasticsearch.service.FileInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (filInfo)表控制层
 *
 * @author LUOY
 */
@RestController
@RequestMapping("/fileInfo")
public class FileInfoController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private FileInfoService fileInfoService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public AjaxResult selectOne(Integer id) {
        return fileInfoService.selectOne(id);
    }


    /**
     *  上传文件附件
     * @param file 文件
     * @return
     */
    @PostMapping("fileUpload")
    public AjaxResult uploadFile(MultipartFile file) {
        return fileInfoService.fileUpload(file);
    }

    /**
     * 删除文件
     * @param fileInfo
     * @return
     */
    @DeleteMapping("fileDelete")
    public AjaxResult fileDelete(@RequestBody FileInfo fileInfo){
        String fileUrl = fileInfo.getFileUrl().substring(fileInfo.getFileUrl().indexOf("/",1));
        String filePath = RuoYiConfig.getProfile() +fileUrl;
        if (FileUtils.deleteFile(filePath)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }
    /**
     *  上传文件附件
     * @param fileInfo 文件信息
     * @return
     */
    @PutMapping("addFileInfo")
    public AjaxResult uploadFile(@RequestBody FileInfo fileInfo) {
         fileInfoService.uploadFileInfo(fileInfo);
        return AjaxResult.success();
    }

    /**
     * 校验文件名是否重复
     * @param fileInfo
     * @return
     */
    @PostMapping("checkFileNameUnique")
    public AjaxResult checkFileNameUnique(@RequestBody FileInfo fileInfo){
        List<FileInfo> fileInfoList = fileInfoService.list(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getFileName,fileInfo.getFileName()).
                eq(FileInfo::getStatus, DataConstants.Number.ZERO).notIn(!FastUtils.checkNull(fileInfo.getId()),FileInfo::getId,fileInfo.getId()));
        if (FastUtils.checkNullOrEmpty(fileInfoList)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(500,"文件名已存在");
        }
    }


    /**
     * 获取所有文件列表内容
     * @param fileInfo
     * @return
     */
    @PostMapping("getAllFIleInfo")
    public AjaxResult getAllFileInfo(@RequestBody FileInfoDto fileInfo){
        return fileInfoService.pageSearchFileInfo(fileInfo,null);
    }

    /**
     * 更新文件信息
     * @param fileInfo
     * @return
     */
    @PutMapping("updateFileInfo")
    public AjaxResult updateFileInfo(@RequestBody FileInfo fileInfo){
         fileInfoService.updateByFileInfoId(fileInfo);
         return AjaxResult.success();
    }

    /**
     * 根据id删除文档信息
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult deleteFileInfo(@PathVariable Integer id){
        return fileInfoService.deleteFileInfoById(id);
    }

}